package com.shlh.saas.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Facebook模板文件生成器独立运行工具类
 * 
 * 可以直接运行此类生成模板文件，而不需要启动整个应用
 */
public class FacebookTemplateGeneratorRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(FacebookTemplateGeneratorRunner.class);
    
    // 模板文件路径
    private static final String TEMPLATE_DIR = "static/templates/facebook/";
    
    // 定义不同的模板文件名
    private static final String COMPANY_NAME_TO_EMAIL = "company_name_to_email.xlsx";
    private static final String COMPANY_NAME_TO_PHONE = "company_name_to_phone.xlsx";
    private static final String NAME_EMAIL_TO_PHONE = "name_email_to_phone.xlsx";
    private static final String PHONE_NAME_TO_EMAIL = "phone_name_to_email.xlsx";
    private static final String DEFAULT_TEMPLATE = "default_template.xlsx";
    
    public static void main(String[] args) {
        System.out.println("开始生成Facebook匹配模板文件...");
        
        try {
            // 获取当前工作目录
            String currentDir = System.getProperty("user.dir");
            String resourcesDir = currentDir + "/src/main/resources/";
            
            // 打印工作目录
            System.out.println("当前工作目录: " + currentDir);
            System.out.println("资源目录: " + resourcesDir);
            
            // 确保模板目录存在
            Files.createDirectories(Paths.get(resourcesDir + TEMPLATE_DIR));
            
            // 生成各种模板
            generateCompanyNameToEmailTemplate(resourcesDir);
            generateCompanyNameToPhoneTemplate(resourcesDir);
            generateNameEmailToPhoneTemplate(resourcesDir);
            generatePhoneNameToEmailTemplate(resourcesDir);
            generateDefaultTemplate(resourcesDir);
            
            System.out.println("所有Facebook匹配模板文件已生成完成！");
            System.out.println("模板存储位置: " + resourcesDir + TEMPLATE_DIR);
        } catch (Exception e) {
            System.err.println("生成模板文件失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 生成企业+姓名匹配邮箱模板
     */
    private static void generateCompanyNameToEmailTemplate(String resourcePath) throws IOException {
        String filePath = resourcePath + TEMPLATE_DIR + COMPANY_NAME_TO_EMAIL;
        
        // 检查文件是否已存在
        if (Files.exists(Paths.get(filePath))) {
            System.out.println("企业+姓名匹配邮箱模板已存在，跳过生成");
            return;
        }
        
        // 创建工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建工作表
            Sheet sheet = workbook.createSheet("企业姓名匹配邮箱");
            
            // 创建表头 - 使用数据库中的确切字段名
            createHeader(workbook, sheet, new String[]{
                "FirstName", "LastName", "Workplace", "Email", "PhoneNumber", 
                "FacebookID", "Gender", "CurrentLocation", "Hometown", 
                "RelationshipStatus", "Date"
            });
            
            // 创建示例数据
            createExampleRow(sheet, 1, new String[]{
                "John", "Doe", "ABC Company", "", "", 
                "", "", "", "", "", ""
            });
            createExampleRow(sheet, 2, new String[]{
                "Jane", "Smith", "XYZ Corporation", "", "", 
                "", "", "", "", "", ""
            });
            
            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            
            System.out.println("企业+姓名匹配邮箱模板生成成功: " + filePath);
        }
    }
    
    /**
     * 生成企业+姓名匹配手机号模板
     */
    private static void generateCompanyNameToPhoneTemplate(String resourcePath) throws IOException {
        String filePath = resourcePath + TEMPLATE_DIR + COMPANY_NAME_TO_PHONE;
        
        // 检查文件是否已存在
        if (Files.exists(Paths.get(filePath))) {
            System.out.println("企业+姓名匹配手机号模板已存在，跳过生成");
            return;
        }
        
        // 创建工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建工作表
            Sheet sheet = workbook.createSheet("企业姓名匹配手机号");
            
            // 创建表头 - 使用数据库中的确切字段名
            createHeader(workbook, sheet, new String[]{
                "FirstName", "LastName", "Workplace", "Email", "PhoneNumber", 
                "FacebookID", "Gender", "CurrentLocation", "Hometown", 
                "RelationshipStatus", "Date"
            });
            
            // 创建示例数据
            createExampleRow(sheet, 1, new String[]{
                "John", "Doe", "ABC Company", "", "", 
                "", "", "", "", "", ""
            });
            createExampleRow(sheet, 2, new String[]{
                "Jane", "Smith", "XYZ Corporation", "", "", 
                "", "", "", "", "", ""
            });
            
            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            
            System.out.println("企业+姓名匹配手机号模板生成成功: " + filePath);
        }
    }
    
    /**
     * 生成姓名+邮箱匹配手机号模板
     */
    private static void generateNameEmailToPhoneTemplate(String resourcePath) throws IOException {
        String filePath = resourcePath + TEMPLATE_DIR + NAME_EMAIL_TO_PHONE;
        
        // 检查文件是否已存在
        if (Files.exists(Paths.get(filePath))) {
            System.out.println("姓名+邮箱匹配手机号模板已存在，跳过生成");
            return;
        }
        
        // 创建工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建工作表
            Sheet sheet = workbook.createSheet("姓名邮箱匹配手机号");
            
            // 创建表头 - 使用数据库中的确切字段名
            createHeader(workbook, sheet, new String[]{
                "FirstName", "LastName", "Workplace", "Email", "PhoneNumber", 
                "FacebookID", "Gender", "CurrentLocation", "Hometown", 
                "RelationshipStatus", "Date"
            });
            
            // 创建示例数据
            createExampleRow(sheet, 1, new String[]{
                "John", "Doe", "", "john.doe@example.com", "", 
                "", "", "", "", "", ""
            });
            createExampleRow(sheet, 2, new String[]{
                "Jane", "Smith", "", "jane.smith@example.com", "", 
                "", "", "", "", "", ""
            });
            
            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            
            System.out.println("姓名+邮箱匹配手机号模板生成成功: " + filePath);
        }
    }
    
    /**
     * 生成手机号+姓名匹配邮箱模板
     */
    private static void generatePhoneNameToEmailTemplate(String resourcePath) throws IOException {
        String filePath = resourcePath + TEMPLATE_DIR + PHONE_NAME_TO_EMAIL;
        
        // 检查文件是否已存在
        if (Files.exists(Paths.get(filePath))) {
            System.out.println("手机号+姓名匹配邮箱模板已存在，跳过生成");
            return;
        }
        
        // 创建工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建工作表
            Sheet sheet = workbook.createSheet("手机号姓名匹配邮箱");
            
            // 创建表头 - 使用数据库中的确切字段名
            createHeader(workbook, sheet, new String[]{
                "FirstName", "LastName", "Workplace", "Email", "PhoneNumber", 
                "FacebookID", "Gender", "CurrentLocation", "Hometown", 
                "RelationshipStatus", "Date"
            });
            
            // 创建示例数据
            createExampleRow(sheet, 1, new String[]{
                "John", "Doe", "", "", "13800138000", 
                "", "", "", "", "", ""
            });
            createExampleRow(sheet, 2, new String[]{
                "Jane", "Smith", "", "", "13900139000", 
                "", "", "", "", "", ""
            });
            
            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            
            System.out.println("手机号+姓名匹配邮箱模板生成成功: " + filePath);
        }
    }
    
    /**
     * 生成默认模板
     */
    private static void generateDefaultTemplate(String resourcePath) throws IOException {
        String filePath = resourcePath + TEMPLATE_DIR + DEFAULT_TEMPLATE;
        
        // 检查文件是否已存在
        if (Files.exists(Paths.get(filePath))) {
            System.out.println("默认匹配模板已存在，跳过生成");
            return;
        }
        
        // 创建工作簿
        try (Workbook workbook = new XSSFWorkbook()) {
            // 创建工作表
            Sheet sheet = workbook.createSheet("匹配模板");
            
            // 创建表头 - 使用数据库中的确切字段名
            createHeader(workbook, sheet, new String[]{
                "PhoneNumber", "FacebookID", "FirstName", "LastName", "Gender", 
                "CurrentLocation", "Hometown", "RelationshipStatus", "Workplace", 
                "Date", "Email"
            });
            
            // 创建示例数据
            createExampleRow(sheet, 1, new String[]{
                "13800138000", "fb123456", "John", "Doe", "M",
                "New York, USA", "Boston, USA", "Single", "ABC Company",
                "", "john.doe@example.com"
            });
            createExampleRow(sheet, 2, new String[]{
                "13900139000", "fb234567", "Jane", "Smith", "F",
                "San Francisco, USA", "Chicago, USA", "Married", "XYZ Corporation",
                "", "jane.smith@example.com"
            });
            
            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
            }
            
            System.out.println("默认匹配模板生成成功: " + filePath);
        }
    }
    
    /**
     * 创建表头行
     */
    private static void createHeader(Workbook workbook, Sheet sheet, String[] headers) {
        // 创建表头行
        Row headerRow = sheet.createRow(0);
        
        // 创建表头单元格样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerStyle.setFont(headerFont);
        
        // 创建表头单元格
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
            sheet.autoSizeColumn(i);
        }
    }
    
    /**
     * 创建示例数据行
     */
    private static void createExampleRow(Sheet sheet, int rowNum, String[] values) {
        Row row = sheet.createRow(rowNum);
        for (int i = 0; i < values.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(values[i]);
        }
    }
} 